package com.jason.kotlin.meta.architecture1.ui

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.jason.kotlin.meta.architecture1.api.MetaImageRepository
import com.jason.kotlin.meta.architecture1.model.MetaImageBean
import com.jason.kotlin.meta.architecture1.model.ResponseWrapper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

typealias ImageLiveData = MutableLiveData<ResponseWrapper<MutableList<MetaImageBean>>>

class MetaImageViewModel : ViewModel() {

    private var currentPage = 0

    private val count = 20

    private val repository =
        MetaImageRepository()

    lateinit var keyword: String

    val data = ImageLiveData()

    private fun loadMetaImage() {
        GlobalScope.launch(Dispatchers.Default) {
            repository.loadMetaImageList(keyword, currentPage, count) {
                data.postValue(
                    ResponseWrapper(
                        true,
                        it,
                        currentPage
                    )
                )
            }
        }

    }

    fun loadData() {
        currentPage = 0
        loadMetaImage()
    }

    fun loadMore() {
        currentPage++
        loadMetaImage()
    }


}

